package com.pine.sunshineelder.eldernurselevel.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.validator.ValidateHelper;
import com.pine.sunshineelder.eldernurselevel.ao.LevelAddAo;
import com.pine.sunshineelder.eldernurselevel.ao.LevelModAo;
import com.pine.sunshineelder.eldernurselevel.service.NurseLevelService;
import com.pine.sunshineelder.common.dao.NurseLevelDao;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.NurseLevelEntity;
import com.pine.sunshineelder.common.exception.ThisSystemException;

@Service
public class NurseLevelServiceImpl implements NurseLevelService, StringHelper {
	@Autowired
	private NurseLevelDao ndao;

	@Override
	public NurseLevelEntity updaterLevel(LevelAddAo ao) throws Exception {
		// 验证清理参数
		// 1.空字符串
		int level = ao.getLevel();
		String name = ValidateHelper.$("名字", ao.getName());
		String content = ValidateHelper.$("护理内容", ao.getContent());
		// 3.结果组装
		NurseLevelEntity nle = new NurseLevelEntity();
		String id = uuid();
		nle.setId(id);
		nle.setLevel(level);
		nle.setName(name);
		nle.setContent(content);
		ndao.insert(nle);
		return nle;
	}

	// 弃用等级
	@Override
	public void deleteLevel(String id) throws Exception {
		// 验证参数
		id = ValidateHelper.$("id", id);
		// 执行业务逻辑
		ndao.delete(id);
	}

	// 启用等级
	@Override
	public void onDeleteLevel(String id) throws Exception {
		// 验证参数
		id = ValidateHelper.$("id", id);
		// 执行业务逻辑
		ndao.onDelete(id);

	}

	@Override
	public NurseLevelEntity modeifLevel(String id, LevelModAo ao) throws Exception {
		// 修改全部
		return null;
	}

	@Override
	public NurseLevelEntity modeifLevel(String id, Map<String, String> map) throws Exception {
		// 1.验证清理参数
		id = ValidateHelper.$("id", id);
		// 2.执行业务逻辑
		// 传入id
		NurseLevelEntity nle = ndao.selectById(id);
		if (nle == null) {
			throw new ThisSystemException("id.not-exisits", "非法id");

		}
		Map<String, Object> nupdateMap = new HashMap<String, Object>();

		// 名字
		String newName = trimAsNull(map.get("name"));
		if (newName != null) {
			// 修改不同内容
			if (!Objects.equals(nle.getName(), newName)) {
				ValidateHelper.assertMatch("名字必须是中文2-16位", newName, "[\u4e00-\u9fa5·]{2,16}");
				// 修改旧对象
				nle.setName(newName);
				// 保存要修改的字段
				nupdateMap.put("name", newName);
			}
		}
		// 等级
		Integer newLevel = parseInt(map.get("level"), null);
		if (newLevel != null) {
			if (nle.getLevel() != newLevel) {
				// 修改旧对象
				nle.setLevel(newLevel);
				// 保存要修改的字段
				nupdateMap.put("level", newLevel);
			}
		}
		// 护理内容

		String newContent = trimAsNull(map.get("content"));
		if (newContent != null) {

			nle.setContent(newContent);
			nupdateMap.put("content", newContent);
		}

		// 进行实际的修改
		if (!nupdateMap.isEmpty()) {
			ndao.update(id, nupdateMap);
		}
		return nle;
	}

	@Override
	public QueryResult queryByKey(String key, Integer pageNo, Integer pageSize) throws Exception {
		key = trimAsNull(key);
		if (key != null) {
			key = "%" + key + "%";
		}

		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		List<NurseLevelEntity> rows = ndao.selectByKey(key, pageNo, pageSize);
		PageInfo pageInfo = new PageInfo(rows);

		int totalRows = (int) pageInfo.getTotal();

		QueryResult result = new QueryResult();
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setRows(rows);
		result.setTotalRows(totalRows);

		return result;
	}

}
